package com.jiangxjsy.core.wrap;

import com.alibaba.fastjson.JSON;
import lombok.Getter;

import java.io.Serializable;

/**
 * wukong-crm-master
 * 蒋胜宇
 * 2025/5/28 19:55
 */

@Getter
public class Result<T> implements Serializable {
    private Integer code;
    private String msg;
    private T data;

    public Result() {
    }

    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(ResultCode resultCode, String msg) {
        this.code = resultCode.getCode();
        this.msg = msg;
    }

    public Result(ResultCode resultCode) {
        this.code = resultCode.getCode();
        this.msg = resultCode.getMsg();
    }

    /**
     * 设置状态码，并返回当前对象
     *
     * @param code 状态码
     * @return 当前对象
     */
    public Result<T> setCode(int code) {
        this.code = code;
        return this;
    }

    /**
     * 设置状态信息，并返回当前对象
     *
     * @param msg 状态信息
     * @return 当前对象
     */
    public Result<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    /**
     * 设置数据，并返回当前对象
     *
     * @param data 数据
     * @return 当前对象
     */
    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }

    /**
     * 转为JSON字符串
     *
     * @return JSON字符串
     */
    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    /**
     * 转为JSON字符串
     *
     * @return JSON字符串
     */
    public String toJSONString() {
        return JSON.toJSONString(this);
    }
}
